package istar.impl;

import istar.IAgent;
import istar.IElement;
import istar.IPosition;
import istar.IRole;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class IstarAgent extends Actor implements IAgent {
	private List<IPosition> occupies = new Vector<IPosition>();
	private List<IRole> plays = new Vector<IRole>();
	
	public IstarAgent(){
		super();
	}
	
	public IstarAgent(String _name){
		super();
		this.setName(_name); 
	}

	@Override
	public List<IPosition> getOccupies() {
		return occupies;
	}

	@Override
	public List<IRole> getPlays() {
		return plays;
	}

	
	
	@Override
	public void addRole(IRole role) {
		this.plays.add(role);		
	}

	@Override
	public void removeRole(IRole role) {
		this.plays.remove(role);
	}

	@Override
	public void addPosition(IPosition position) {
		this.occupies.add(position);
		this.plays.addAll(position.getCovers());
		
	}

	@Override
	public void removePosition(IPosition position) {
		this.occupies.remove(position);
		this.plays.removeAll(position.getCovers());
		
	}


	@Override
	public boolean able(IElement elmnt) {
		if (super.able(elmnt))
			return true;
		Iterator<IRole> itRoles = this.getPlays().iterator();
		while (itRoles.hasNext()){
			IRole role = itRoles.next();
			if (elmnt.able(role))
				return true;
		}
		return false;
	}

	@Override
	public boolean workable(IElement elmnt) {
		if (super.workable(elmnt))
			return true;
		Iterator<IRole> itRoles = this.getPlays().iterator();
		while (itRoles.hasNext()){
			IRole role = itRoles.next();
			if (elmnt.workable(role))
				return true;
		}
		return false;
	}
	
	
}
